PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

About Script Objects

A script object is a user-defined object that combines data (in the form of properties) and potential actions (in the form of handlers). A script object definition is a compound statement that can contain collections of properties, handlers, and other AppleScript statements.

Here is a simple script object definition:

script John
    property HowManyTimes : 0
    to sayHello to someone
        set HowManyTimes to HowManyTimes + 1
        return "Hello " & someone
    end sayHello
end script

It defines a script object that can handle the sayHello command. It assigns the script object to the variable John . The definition includes a handler for the sayHello command. It also includes a property, called HowManyTimes, that indicates how many times the sayHello command has been called.

A handler within a script object definition follows the same syntax rules as a subroutine definition. Unlike a subroutine definition, however, you can group a handler within a script object definition with properties whose values are related to the handler's actions.

After you define a script object, you initialize it by running the script that contains the script object definition. You can then use a Tell statement to send commands to the script object. For example, the following statement sends the sayHello command the script object defined above.

tell John to sayHello to "Herb"

The result is "Hello Herb" .

You can manipulate the properties of script objects in the same way you manipulate the properties of system and application objects. Use the Get command to get the value of a property and the Set or Copy command to change the value of a property.

The following statement uses a Get command to get the value of the HowManyTimes property of script object John .

get HowManyTimes of John
if the result > 10
    return "John, aren't you tired of saying hello?"
end if

© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)